# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from TimerThread import TimerThread
from PyQt4 import QtCore

## CONTROLLER
#_______________________________________________________________________________
class PlayerController(QtCore.QObject):
    def __init__(self, server, view):
        QtCore.QObject.__init__(self)
        self.server_model = server.model
        self.server_ctrl = server
        self.view = view
        self.id_timerevent = None
        #Signal
        self.server_ctrl.client_connected.connect(self.setup)
        self.server_ctrl.player_started.connect(self.played)
        self.server_ctrl.player_paused.connect(self.paused)
        self.server_ctrl.player_stopped.connect(self.stopped)
        self.server_ctrl.player_song_changed.connect(self.skipped)
        self.server_ctrl.player_song_seeked.connect(self.seeked)
        self.server_ctrl.mixer_volume_changed.connect(self.changed_volume)
        self.view.play_button.button_clicked.connect(self.play_pause)
        self.view.stop_button.clicked.connect(self.stop)
        self.view.prev_button.clicked.connect(self.prev)
        self.view.next_button.clicked.connect(self.next)
        self.view.volume_button.volume_muted.connect(self.mute)
        self.view.progress_bar.pos_seeked.connect(self.seek)
        self.view.volume_bar.volume_changed.connect(self.set_volume)
    
    def setup(self, connected):
        if connected is True:
            self.view.play_button.setEnabled(True)
            self.view.volume_button.setEnabled(True)
            self.view.progress_bar.setEnabled(True)
            self.view.volume_bar.setEnabled(True)
            self.view.next_button.setEnabled(True)
            self.view.prev_button.setEnabled(True)
            self.view.stop_button.setEnabled(True)
            self.view.volume_button.setEnabled(True)
            if self.server_model.info_song:
                time_total = self.server_model.info_song["time"]
            else:
                time_total = 0
            volume = self.server_model.info_status["volume"]["value"]
            self.update_progressbar_value(0)
            self.update_progressbar_maximum(time_total)
            self.update_volumebar_value(volume)
            self.timer = TimerThread(self.timer_event)            
        else:
            if self.timer.isRunning():    
                self.timer.exit_()
            self.update_progressbar_value()
            self.update_volumebar_value()
            self.view.volume_button.setEnabled(False)
            self.view.stop_button.setEnabled(False)
            self.view.next_button.setEnabled(False) 
            self.view.prev_button.setEnabled(False)
            self.view.play_button.setEnabled(False)

    def update_progressbar_value(self, time=None):
        if time is None:
            self.view.progress_bar.setEnabled(False)
        else:
            self.view.progress_bar.setValue(time)
    
    def update_progressbar_maximum(self, time):
        self.view.progress_bar.setMaximum(time)
    
    def update_volumebar_value(self, volume=None):
        if volume is None:
            self.view.volume_bar.setEnabled(False)
        else:
            if self.view.volume_bar.value() != volume:
                self.view.volume_bar.setValue(volume)
            
    def play_pause(self):
        if self.server_model.info_status["state"] != "play":
            self.server_ctrl.client.playid() 
        elif self.server_model.info_status["state"] == "play":
            self.server_ctrl.client.pause()
    
    def seek(self, value):
        if self.server_model.info_status["state"] == "stop":
            return
        self.server_ctrl.client.seekid(self.server_model.info_song["id"], value)
    
    def set_volume(self, value):
        """FIXME: provare a velocizzare"""
        self.server_ctrl.client.setvol(value)
        if self.server_model.info_status["volume"]["muted"] is True:
            self.server_model.info_status["volume"]["muted"] = False
            self.view.volume_button.toggle_icon()
    
    def mute(self):
        if self.server_model.info_status["volume"]["muted"] is False:
            self.server_ctrl.client.setvol(0)
            self.server_model.info_status["volume"]["muted"] = True
        else:
            volume = self.server_model.info_status["volume"]["value"]
            self.server_ctrl.client.setvol(volume)
            self.server_model.info_status["volume"]["muted"] = False
    
    def next(self):
        if self.server_model.info_status["state"] == "stop":
            return
        self.server_ctrl.client.next()
    
    def prev(self):
        if self.server_model.info_status["state"] == "stop":
            return
        self.server_ctrl.client.previous()
    
    def stop(self):
        if self.server_model.info_status["state"] == "stop":
            return
        self.server_ctrl.client.stop()
    
    def played(self):
#        self.id_timerevent = self.startTimer(500)
        self.timer.start()
        self.view.play_button.toggle_icon("play")
        time_total = self.server_model.info_song["time"]
        self.update_progressbar_maximum(time_total)
        
    def stopped(self):
        if self.timer.isRunning():
            self.timer.exit_()
        self.view.play_button.toggle_icon("stop")
        self.update_progressbar_value(0)
    
    def paused(self):
        if self.timer.isRunning():
            self.timer.exit_()
        self.view.play_button.toggle_icon("pause")
        time = self.server_model.info_status["time"]
        self.update_progressbar_value(time)
    
    def skipped(self):
        time_total = self.server_model.info_song["time"]
        self.update_progressbar_maximum(time_total)
    
    def seeked(self):
        time = self.server_model.info_status["time"]
        self.update_progressbar_value(time)
    
    def changed_volume(self):
        if self.server_model.info_status["volume"]["muted"] is True:
            return
        volume = self.server_model.info_status["volume"]["value"]
        self.update_volumebar_value(volume)
    
    def timer_event(self):
        info = self.server_ctrl.client.status()
        if info["state"] == "play":
            time = int(info["time"].split(":")[0])
            self.update_progressbar_value(time)